#!/bin/zsh
# -*-coding: latin-1;-*-

##########################################################
#
# title:     zsh main config file
#
# filename:  /etc/zsh/zshrc
#
# author(s): Alessandro Molari (mola89.molarulez.com),
#            Luca Molari
#
# license:   This file is licensed under the GPLv2
#            For more informations read:
#            http://www.gnu.org/licenses/gpl-2.0.txt
#
# thanks to: zsh team (www.zsh.org)
#            grml team (grml.org)
#            Michael Prokop (mika@grml.org)
#
##########################################################



##         initial setup

# cd to home on first invocation of zsh
#     (thanks to Bart Schaefer)
if [ -z $ZSH_ALREADYCDHOME ]; then
    export ZSH_ALREADYCDHOME=$HOME
    cd
fi

# default permissions setup
#     non-root: 022
#                   files: rw-r--r--  =>  666 - 022 = 644
#                   dirs:  rwxr-xr-x  =>  777 - 022 = 755
#     root:     022
#                   files: rw-r--r--  =>  666 - 022 = 644
#                   dirs:  rwxr-xr-x  =>  777 - 022 = 755
if [ $EUID -ne 0 ]; then
    # non-root case
    umask 022
else
    # root case
    umask 022
fi

## end of: initial setup


##         functions

# load utility functions
if [ -r $ZSHDIR_GLOBAL/functions/utility ]; then
    source $ZSHDIR_GLOBAL/functions/utility
fi

## end of: functions


##         autoloading

if [ -r $ZSHDIR_GLOBAL/autoload ]; then
    source $ZSHDIR_GLOBAL/autoload
fi

## end of: autoloading


##         options

# load the options
if [ -r $ZSHDIR_GLOBAL/options ]; then
    source $ZSHDIR_GLOBAL/options
fi

## end of: options


##         completion

# prevent unwanted corrections
if [ -r $ZSHDIR_GLOBAL/completion/acceptline_wrapper ]; then
    source $ZSHDIR_GLOBAL/completion/acceptline_wrapper
fi

# load completion system
if [ -r $ZSHDIR_GLOBAL/completion/comp_system ]; then
    source $ZSHDIR_GLOBAL/completion/comp_system
fi

## end of: completion


##         profiling

# load zprof (zshell profiling) if ZSH_PROFILE is setted to 1
if [[ $ZSH_PROFILE -eq 1 ]]; then
   zmodload zsh/zprof
fi

## end of: profiling


##         history

HISTSIZE=5000
SAVEHIST=5000

## end of: history


##         directories

# dirstack handling
if [[ -f ${DIRSTACKFILE} ]] && [[ ${#dirstack[*]} -eq 0 ]] ; then
    dirstack=( ${(f)"$(< $DIRSTACKFILE)"} )
    [[ -d $dirstack[0] ]] && cd $dirstack[0] && cd $OLDPWD
fi
chpwd() {
    local -ax my_stack
    my_stack=( ${PWD} ${dirstack} )
    builtin print -l ${(u)my_stack} >! ${DIRSTACKFILE}
    print $my_stack >! ${DIRSTACKFILE}
}

# hash some often used directories
if [ -d $USERDIR_WORKDIR ]; then
    hash -d workdir=$USERDIR_WORKDIR
fi
if [ -d $USERDIR_TMP ]; then
    hash -d temp=$USERDIR_TMP
fi
if [ -d $USERDIR_DOCS ]; then
    hash -d docs=$USERDIR_DOCS
fi
if [ -d $USERDIR_GRAPHICS ]; then
    hash -d graphics=$USERDIR_GRAPHICS
fi
if [ -d $USERDIR_AUDIO ]; then
    hash -d audio=$USERDIR_AUDIO
fi
if [ -d $USERDIR_VIDEO ]; then
    hash -d video=$USERDIR_VIDEO
fi
if [ -d $USERDIR_DEV ]; then
    hash -d dev=$USERDIR_DEV
fi
if [ -d $USERDIR_INCOMING ]; then
    hash -d incoming=$USERDIR_INCOMING
fi
if [ -d $USERDIR_DOWNLOADS ]; then
    hash -d downloads=$USERDIR_DOWNLOADS
fi
if [ -d $USERDIR_PUBLIC ]; then
    hash -d public=$USERDIR_PUBLIC
fi

## end of: directories


##         colors

# setup colors
if [ -r /etc/zsh/colors ]; then
    source $ZSHDIR_GLOBAL/colors
fi

## end of: colors


##         keybindings

# setup keybindings
if [ -r $ZSHDIR_GLOBAL/keybindings ]; then
    source $ZSHDIR_GLOBAL/keybindings
fi

## end of: keybindings


##         aliases

# setup global and suffix aliases
if [ -r $ZSHDIR_GLOBAL/aliases/gs_aliases ]; then
    source $ZSHDIR_GLOBAL/aliases/gs_aliases
fi

# setup the abbrevations completion
if [ -r $ZSHDIR_GLOBAL/aliases/power_comp ]; then
    source $ZSHDIR_GLOBAL/aliases/power_comp
fi

## end of: aliases


##         prompt

# setup the left and right prompt
if [ -r $ZSHDIR_GLOBAL/prompt ]; then
    source $ZSHDIR_GLOBAL/prompt
fi

## end of: prompt


##         cleaning

# remove duplicates
typeset -U path cdpath fpath manpath

# clean up colors
for color in BLUE RED GREEN CYAN YELLOW MAGENTA WHITE ; unset $color

## end of: cleaning

